package com.alibaba.springboot.health.shiro;

import com.alibaba.springboot.health.pojo.Permission;
import com.alibaba.springboot.health.pojo.Role;
import com.alibaba.springboot.health.pojo.User;
import com.alibaba.springboot.health.service.PermissionService;
import com.alibaba.springboot.health.service.RoleService;
import com.alibaba.springboot.health.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {
private static final Logger LOGGER= LogManager.getLogger(MyRealm.class);
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleService roleService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取用户信息
        String userName = (String) principalCollection.getPrimaryPrincipal();
        //通过用户信息查询拥有的权限
        LOGGER.info(userName+"========11111111111111111111111111111111111111");
        List<Permission> permissions = permissionService.queryByUserName(userName);
        Role role = roleService.queryByUserName(userName);
        LOGGER.info(role+"========11111111111111111111111111111111111111");
        if (permissions != null){
            Set<String> perms = new HashSet<>();
            for (Permission perm : permissions) {
                LOGGER.info("===================------------------------------"+perm.getMenuUrl());
                perms.add(perm.getMenuUrl());
            }
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

            authorizationInfo.setStringPermissions(perms);

            authorizationInfo.addRole(role.getRoleName());

            return authorizationInfo;
        }
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户信息
        String userName = (String) token.getPrincipal();
        //获取数据库此用户的密码
        User user = userService.queryByName(userName);
        if (user != null) {
            //从数据库获取用户密码
            String password = user.getPassword();
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, password, this.getName());
            return authenticationInfo;
        }
        return null;
    }
}
